﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using GalaSoft.MvvmLight;
namespace GoogleReaderToDelicious.WPF.Framework
{
   
    public abstract class AppViewModelBase :ViewModelBase
    {


        protected AppViewModelBase()
        {
        }
          
     
        public void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            RaisePropertyChanged(ExtractPropertyName(propertyExpression));

        }
        public string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression == null)
            {
                throw new ArgumentException("propertyExpression");
            }

            var property = memberExpression.Member as PropertyInfo;
            if (property == null)
            {
                throw new ArgumentException("propertyExpression");
            }

            //if (!property.DeclaringType.IsAssignableFrom(GetType()))
            //{
            //    throw new ArgumentException("propertyExpression");
            //}

            var getMethod = property.GetGetMethod(true);
            if (getMethod == null)
            {
                // this shouldn't happen - the expression would reject the property before reaching this far
                throw new ArgumentException("propertyExpression");
            }

            if (getMethod.IsStatic)
            {
                throw new ArgumentException("propertyExpression");
            }

            return memberExpression.Member.Name;
        }

    }
}